setwd("~/Dropbox/DemBackslidingStates/MTurk")
library(dotwhisker)
library(ggplot2);library(dplyr);library(cregg)
library(haven);library(car);library(stargazer);library(cjoint)

##################
##PREPARE DATA####
##################

##PREP STUDENT DATA
setwd("~/Dropbox/DemBackslidingStates/MTurk")
library(haven);library(car);library(stargazer);library(cjoint)
d <-  read.csv("DemBacksliding_MTURK.csv")
dim(d)

d$pid <-NA
d$pid[d$PID2==1] <- 1
d$pid[d$PID2==2] <- 2
d$pid[d$PID4==2] <- 3
d$pid[d$PID4==3] <- 4
d$pid[d$PID4==1] <- 5
d$pid[d$PID3==2] <- 6
d$pid[d$PID3==1] <- 7
table(d$pid)

#Table A1 [MTurk]
prop.table(table((d$GENDER)))
prop.table(table((d$RACE)))
prop.table(table((d$HISP)))
prop.table(table((d$EDUC)))
d$age <- 2019-d$YRBORN
mean(d$age, na.rm=T)
mean(d$pid, na.rm=T)
mean(d$IDEO, na.rm=T)

df <- setNames(data.frame(matrix(ncol = 12, nrow = dim(d)[1]*30)), c("responseId","respondent","trial","c_salary","c_size", "c_rural", "c_comp", "c_pol", "c_bs","c_rate","c_choose","pid"))
d$ResponseId <- as.character(d$ResponseId)
respondent <- 1
row <- 1
for (respondent in 1:dim(d)[1]){
  df$responseId[row] <- d$ResponseId[respondent]
  df$responseId[row+1] <- d$ResponseId[respondent]
  df$responseId[row+2] <- d$ResponseId[respondent]
  df$responseId[row+3] <- d$ResponseId[respondent]
  df$responseId[row+4] <- d$ResponseId[respondent]
  df$responseId[row+5] <- d$ResponseId[respondent]
  df$responseId[row+6] <- d$ResponseId[respondent]
  df$responseId[row+7] <- d$ResponseId[respondent]
  df$responseId[row+8] <- d$ResponseId[respondent]
  df$responseId[row+9] <- d$ResponseId[respondent]
  df$responseId[row+10] <- d$ResponseId[respondent]
  df$responseId[row+11] <- d$ResponseId[respondent]
  df$responseId[row+12] <- d$ResponseId[respondent]
  df$responseId[row+13] <- d$ResponseId[respondent]
  df$responseId[row+14] <- d$ResponseId[respondent]
  df$responseId[row+15] <- d$ResponseId[respondent]
  df$responseId[row+16] <- d$ResponseId[respondent]
  df$responseId[row+17] <- d$ResponseId[respondent]
  df$responseId[row+18] <- d$ResponseId[respondent]
  df$responseId[row+19] <- d$ResponseId[respondent]
  df$responseId[row+20] <- d$ResponseId[respondent]
  df$responseId[row+21] <- d$ResponseId[respondent]
  df$responseId[row+22] <- d$ResponseId[respondent]
  df$responseId[row+23] <- d$ResponseId[respondent]
  df$responseId[row+24] <- d$ResponseId[respondent]
  df$responseId[row+25] <- d$ResponseId[respondent]
  df$responseId[row+26] <- d$ResponseId[respondent]
  df$responseId[row+27] <- d$ResponseId[respondent]
  df$responseId[row+28] <- d$ResponseId[respondent]
  df$responseId[row+29] <- d$ResponseId[respondent]
  df$respondent[row] <- respondent
  df$respondent[row+1] <- respondent
  df$respondent[row+2] <- respondent
  df$respondent[row+3] <- respondent
  df$respondent[row+4] <- respondent
  df$respondent[row+5] <- respondent
  df$respondent[row+6] <- respondent
  df$respondent[row+7] <- respondent
  df$respondent[row+8] <- respondent
  df$respondent[row+9] <- respondent
  df$respondent[row+10] <- respondent
  df$respondent[row+11] <- respondent
  df$respondent[row+12] <- respondent
  df$respondent[row+13] <- respondent
  df$respondent[row+14] <- respondent
  df$respondent[row+15] <- respondent
  df$respondent[row+16] <- respondent
  df$respondent[row+17] <- respondent
  df$respondent[row+18] <- respondent
  df$respondent[row+19] <- respondent
  df$respondent[row+20] <- respondent
  df$respondent[row+21] <- respondent
  df$respondent[row+22] <- respondent
  df$respondent[row+23] <- respondent
  df$respondent[row+24] <- respondent
  df$respondent[row+25] <- respondent
  df$respondent[row+26] <- respondent
  df$respondent[row+27] <- respondent
  df$respondent[row+28] <- respondent
  df$respondent[row+29] <- respondent
  df$trial[row] <- 1
  df$trial[row+1] <- 1
  df$trial[row+2] <- 2
  df$trial[row+3] <- 2
  df$trial[row+4] <- 3
  df$trial[row+5] <- 3
  df$trial[row+6] <- 4
  df$trial[row+7] <- 4
  df$trial[row+8] <- 5
  df$trial[row+9] <- 5
  df$trial[row+10] <- 6
  df$trial[row+11] <- 6
  df$trial[row+12] <- 7
  df$trial[row+13] <- 7
  df$trial[row+14] <- 8
  df$trial[row+15] <- 8
  df$trial[row+16] <- 9
  df$trial[row+17] <- 9
  df$trial[row+18] <- 10
  df$trial[row+19] <- 10
  df$trial[row+20] <- 11
  df$trial[row+21] <- 11
  df$trial[row+22] <- 12
  df$trial[row+23] <- 12
  df$trial[row+24] <- 13
  df$trial[row+25] <- 13
  df$trial[row+26] <- 14
  df$trial[row+27] <- 14
  df$trial[row+28] <- 15
  df$trial[row+29] <- 15
  df$c_number[row] <- 1
  df$c_number[row+1] <- 2
  df$c_number[row+2] <- 1
  df$c_number[row+3] <- 2
  df$c_number[row+4] <- 1
  df$c_number[row+5] <- 2
  df$c_number[row+6] <- 1
  df$c_number[row+7] <- 2
  df$c_number[row+8] <- 1
  df$c_number[row+9] <- 2
  df$c_number[row+10] <- 1
  df$c_number[row+11] <- 2
  df$c_number[row+12] <- 1
  df$c_number[row+13] <- 2
  df$c_number[row+14] <- 1
  df$c_number[row+15] <- 2
  df$c_number[row+16] <- 1
  df$c_number[row+17] <- 2
  df$c_number[row+18] <- 1
  df$c_number[row+19] <- 2
  df$c_number[row+20] <- 1
  df$c_number[row+21] <- 2
  df$c_number[row+22] <- 1
  df$c_number[row+23] <- 2
  df$c_number[row+24] <- 1
  df$c_number[row+25] <- 2
  df$c_number[row+26] <- 1
  df$c_number[row+27] <- 2
  df$c_number[row+28] <- 1
  df$c_number[row+29] <- 2
  
  df$pid[row] <- d$pid[respondent]
  df$pid[row+1] <- d$pid[respondent]
  df$pid[row+2] <- d$pid[respondent]
  df$pid[row+3] <- d$pid[respondent]
  df$pid[row+4] <- d$pid[respondent]
  df$pid[row+5] <- d$pid[respondent]
  df$pid[row+6] <- d$pid[respondent]
  df$pid[row+7] <- d$pid[respondent]
  df$pid[row+8] <- d$pid[respondent]
  df$pid[row+9] <- d$pid[respondent]
  df$pid[row+10] <- d$pid[respondent]
  df$pid[row+11] <- d$pid[respondent]
  df$pid[row+12] <- d$pid[respondent]
  df$pid[row+13] <- d$pid[respondent]
  df$pid[row+14] <- d$pid[respondent]
  df$pid[row+15] <- d$pid[respondent]
  df$pid[row+16] <- d$pid[respondent]
  df$pid[row+17] <- d$pid[respondent]
  df$pid[row+18] <- d$pid[respondent]
  df$pid[row+19] <- d$pid[respondent]
  df$pid[row+20] <- d$pid[respondent]
  df$pid[row+21] <- d$pid[respondent]
  df$pid[row+22] <- d$pid[respondent]
  df$pid[row+23] <- d$pid[respondent]
  df$pid[row+24] <- d$pid[respondent]
  df$pid[row+25] <- d$pid[respondent]
  df$pid[row+26] <- d$pid[respondent]
  df$pid[row+27] <- d$pid[respondent]
  df$pid[row+28] <- d$pid[respondent]
  df$pid[row+29] <- d$pid[respondent]
  
  #Trial 1
  df$c_salary[row] <- d$sal1_DO[respondent]
  df$c_size[row] <- d$size1_DO[respondent]
  df$c_rural[row] <- d$rural1_DO[respondent]
  df$c_comp[row] <- d$comp1_DO[respondent]
  df$c_pol[row] <- d$pol1_DO[respondent]
  df$c_bs[row] <- d$bs1_DO[respondent]
  df$c_rate[row] <- d$offer1_1[respondent]
  df$c_choose[row] <- d$accept_1[respondent]
  
  df$c_salary[row+1] <- d$sal2_DO[respondent]
  df$c_size[row+1] <- d$size2_DO[respondent]
  df$c_rural[row+1] <- d$rural2_DO[respondent]
  df$c_comp[row+1] <- d$comp2_DO[respondent]
  df$c_pol[row+1] <- d$pol2_DO[respondent]
  df$c_bs[row+1] <- d$bs2_DO[respondent]
  df$c_rate[row+1] <- d$offer2_1[respondent]
  df$c_choose[row+1] <- 3-d$accept_1[respondent]  
  
  #Trial 2
  df$c_salary[row+2] <- d$sal3_DO[respondent]
  df$c_size[row+2] <- d$size3_DO[respondent]
  df$c_rural[row+2] <- d$rural3_DO[respondent]
  df$c_comp[row+2] <- d$comp3_DO[respondent]
  df$c_pol[row+2] <- d$pol3_DO[respondent]
  df$c_bs[row+2] <- d$bs3_DO[respondent]
  df$c_rate[row+2] <- d$offer1_2[respondent]
  df$c_choose[row+2] <- d$accept_2[respondent]
  
  df$c_salary[row+3] <- d$sal4_DO[respondent]
  df$c_size[row+3] <- d$size4_DO[respondent]
  df$c_rural[row+3] <- d$rural4_DO[respondent]
  df$c_comp[row+3] <- d$comp4_DO[respondent]
  df$c_pol[row+3] <- d$pol4_DO[respondent]
  df$c_bs[row+3] <- d$bs4_DO[respondent]
  df$c_rate[row+3] <- d$offer2_2[respondent]
  df$c_choose[row+3] <- 3-d$accept_2[respondent]  
  
  #Trial 3
  df$c_salary[row+4] <- d$sal5_DO[respondent]
  df$c_size[row+4] <- d$size5_DO[respondent]
  df$c_rural[row+4] <- d$rural5_DO[respondent]
  df$c_comp[row+4] <- d$comp5_DO[respondent]
  df$c_pol[row+4] <- d$pol5_DO[respondent]
  df$c_bs[row+4] <- d$bs5_DO[respondent]
  df$c_rate[row+4] <- d$offer1_3[respondent]
  df$c_choose[row+4] <- d$accept_3[respondent]
  
  df$c_salary[row+5] <- d$sal6_DO[respondent]
  df$c_size[row+5] <- d$size6_DO[respondent]
  df$c_rural[row+5] <- d$rural6_DO[respondent]
  df$c_comp[row+5] <- d$comp6_DO[respondent]
  df$c_pol[row+5] <- d$pol6_DO[respondent]
  df$c_bs[row+5] <- d$bs6_DO[respondent]
  df$c_rate[row+5] <- d$offer2_3[respondent]
  df$c_choose[row+5] <- 3-d$accept_3[respondent]  
  
  #Trial 4
  df$c_salary[row+6] <- d$sal7_DO[respondent]
  df$c_size[row+6] <- d$size7_DO[respondent]
  df$c_rural[row+6] <- d$rural7_DO[respondent]
  df$c_comp[row+6] <- d$comp7_DO[respondent]
  df$c_pol[row+6] <- d$pol7_DO[respondent]
  df$c_bs[row+6] <- d$bs7_DO[respondent]
  df$c_rate[row+6] <- d$offer1_4[respondent]
  df$c_choose[row+6] <- d$accept_4[respondent]
  
  df$c_salary[row+7] <- d$sal8_DO[respondent]
  df$c_size[row+7] <- d$size8_DO[respondent]
  df$c_rural[row+7] <- d$rural8_DO[respondent]
  df$c_comp[row+7] <- d$comp8_DO[respondent]
  df$c_pol[row+7] <- d$pol8_DO[respondent]
  df$c_bs[row+7] <- d$bs8_DO[respondent]
  df$c_rate[row+7] <- d$offer2_4[respondent]
  df$c_choose[row+7] <- 3-d$accept_4[respondent]  
  
  #Trial 5
  df$c_salary[row+8] <- d$sal9_DO[respondent]
  df$c_size[row+8] <- d$size9_DO[respondent]
  df$c_rural[row+8] <- d$rural9_DO[respondent]
  df$c_comp[row+8] <- d$comp9_DO[respondent]
  df$c_pol[row+8] <- d$pol9_DO[respondent]
  df$c_bs[row+8] <- d$bs9_DO[respondent]
  df$c_rate[row+8] <- d$offer1_5[respondent]
  df$c_choose[row+8] <- d$accept_9[respondent]
  
  df$c_salary[row+9] <- d$sal10_DO[respondent]
  df$c_size[row+9] <- d$size10_DO[respondent]
  df$c_rural[row+9] <- d$rural10_DO[respondent]
  df$c_comp[row+9] <- d$comp10_DO[respondent]
  df$c_pol[row+9] <- d$pol10_DO[respondent]
  df$c_bs[row+9] <- d$bs10_DO[respondent]
  df$c_rate[row+9] <- d$offer2_5[respondent]
  df$c_choose[row+9] <- 3-d$accept_5[respondent]  
  
  #Trial 6
  df$c_salary[row+10] <- d$sal11_DO[respondent]
  df$c_size[row+10] <- d$size11_DO[respondent]
  df$c_rural[row+10] <- d$rural11_DO[respondent]
  df$c_comp[row+10] <- d$comp11_DO[respondent]
  df$c_pol[row+10] <- d$pol11_DO[respondent]
  df$c_bs[row+10] <- d$bs11_DO[respondent]
  df$c_rate[row+10] <- d$offer1_6[respondent]
  df$c_choose[row+10] <- d$accept_6[respondent]
  
  df$c_salary[row+11] <- d$sal12_DO[respondent]
  df$c_size[row+11] <- d$size12_DO[respondent]
  df$c_rural[row+11] <- d$rural12_DO[respondent]
  df$c_comp[row+11] <- d$comp12_DO[respondent]
  df$c_pol[row+11] <- d$pol12_DO[respondent]
  df$c_bs[row+11] <- d$bs12_DO[respondent]
  df$c_rate[row+11] <- d$offer2_6[respondent]
  df$c_choose[row+11] <- 3-d$accept_6[respondent]  
  
  #Trial 7
  df$c_salary[row+12] <- d$sal13_DO[respondent]
  df$c_size[row+12] <- d$size13_DO[respondent]
  df$c_rural[row+12] <- d$rural13_DO[respondent]
  df$c_comp[row+12] <- d$comp13_DO[respondent]
  df$c_pol[row+12] <- d$pol13_DO[respondent]
  df$c_bs[row+12] <- d$bs13_DO[respondent]
  df$c_rate[row+12] <- d$offer1_7[respondent]
  df$c_choose[row+12] <- d$accept_7[respondent]
  
  df$c_salary[row+13] <- d$sal14_DO[respondent]
  df$c_size[row+13] <- d$size14_DO[respondent]
  df$c_rural[row+13] <- d$rural14_DO[respondent]
  df$c_comp[row+13] <- d$comp14_DO[respondent]
  df$c_pol[row+13] <- d$pol14_DO[respondent]
  df$c_bs[row+13] <- d$bs14_DO[respondent]
  df$c_rate[row+13] <- d$offer2_7[respondent]
  df$c_choose[row+13] <- 3-d$accept_7[respondent]  
  
  #Trial 8
  df$c_salary[row+14] <- d$sal15_DO[respondent]
  df$c_size[row+14] <- d$size15_DO[respondent]
  df$c_rural[row+14] <- d$rural15_DO[respondent]
  df$c_comp[row+14] <- d$comp15_DO[respondent]
  df$c_pol[row+14] <- d$pol15_DO[respondent]
  df$c_bs[row+14] <- d$bs15_DO[respondent]
  df$c_rate[row+14] <- d$offer1_8[respondent]
  df$c_choose[row+14] <- d$accept_8[respondent]
  
  df$c_salary[row+15] <- d$sal16_DO[respondent]
  df$c_size[row+15] <- d$size16_DO[respondent]
  df$c_rural[row+15] <- d$rural16_DO[respondent]
  df$c_comp[row+15] <- d$comp16_DO[respondent]
  df$c_pol[row+15] <- d$pol16_DO[respondent]
  df$c_bs[row+15] <- d$bs16_DO[respondent]
  df$c_rate[row+15] <- d$offer2_8[respondent]
  df$c_choose[row+15] <- 3-d$accept_8[respondent]  
  
  #Trial 9
  df$c_salary[row+16] <- d$sal17_DO[respondent]
  df$c_size[row+16] <- d$size17_DO[respondent]
  df$c_rural[row+16] <- d$rural17_DO[respondent]
  df$c_comp[row+16] <- d$comp17_DO[respondent]
  df$c_pol[row+16] <- d$pol17_DO[respondent]
  df$c_bs[row+16] <- d$bs17_DO[respondent]
  df$c_rate[row+16] <- d$offer1_9[respondent]
  df$c_choose[row+16] <- d$accept_9[respondent]
  
  df$c_salary[row+17] <- d$sal18_DO[respondent]
  df$c_size[row+17] <- d$size18_DO[respondent]
  df$c_rural[row+17] <- d$rural18_DO[respondent]
  df$c_comp[row+17] <- d$comp18_DO[respondent]
  df$c_pol[row+17] <- d$pol18_DO[respondent]
  df$c_bs[row+17] <- d$bs18_DO[respondent]
  df$c_rate[row+17] <- d$offer2_9[respondent]
  df$c_choose[row+17] <- 3-d$accept_9[respondent]  
  
  #Trial 10
  df$c_salary[row+18] <- d$sal19_DO[respondent]
  df$c_size[row+18] <- d$size19_DO[respondent]
  df$c_rural[row+18] <- d$rural19_DO[respondent]
  df$c_comp[row+18] <- d$comp19_DO[respondent]
  df$c_pol[row+18] <- d$pol19_DO[respondent]
  df$c_bs[row+18] <- d$bs19_DO[respondent]
  df$c_rate[row+18] <- d$offer1_10[respondent]
  df$c_choose[row+18] <- d$accept_10[respondent]
  
  df$c_salary[row+19] <- d$sal20_DO[respondent]
  df$c_size[row+19] <- d$size20_DO[respondent]
  df$c_rural[row+19] <- d$rural20_DO[respondent]
  df$c_comp[row+19] <- d$comp20_DO[respondent]
  df$c_pol[row+19] <- d$pol20_DO[respondent]
  df$c_bs[row+19] <- d$bs20_DO[respondent]
  df$c_rate[row+19] <- d$offer2_10[respondent]
  df$c_choose[row+19] <- 3-d$accept_10[respondent]  
  
  #Trial 11
  df$c_salary[row+20] <- d$sal21_DO[respondent]
  df$c_size[row+20] <- d$size21_DO[respondent]
  df$c_rural[row+20] <- d$rural21_DO[respondent]
  df$c_comp[row+20] <- d$comp21_DO[respondent]
  df$c_pol[row+20] <- d$pol21_DO[respondent]
  df$c_bs[row+20] <- d$bs21_DO[respondent]
  df$c_rate[row+20] <- d$offer1_11[respondent]
  df$c_choose[row+20] <- d$accept_11[respondent]
  
  df$c_salary[row+21] <- d$sal22_DO[respondent]
  df$c_size[row+21] <- d$size22_DO[respondent]
  df$c_rural[row+21] <- d$rural22_DO[respondent]
  df$c_comp[row+21] <- d$comp22_DO[respondent]
  df$c_pol[row+21] <- d$pol2_DO[respondent]
  df$c_bs[row+21] <- d$bs22_DO[respondent]
  df$c_rate[row+21] <- d$offer2_11[respondent]
  df$c_choose[row+21] <- 3-d$accept_11[respondent]    
  
  #Trial 12
  df$c_salary[row+22] <- d$sal23_DO[respondent]
  df$c_size[row+22] <- d$size23_DO[respondent]
  df$c_rural[row+22] <- d$rural23_DO[respondent]
  df$c_comp[row+22] <- d$comp23_DO[respondent]
  df$c_pol[row+22] <- d$pol23_DO[respondent]
  df$c_bs[row+22] <- d$bs23_DO[respondent]
  df$c_rate[row+22] <- d$offer1_12[respondent]
  df$c_choose[row+22] <- d$accept_12[respondent]
  
  df$c_salary[row+23] <- d$sal24_DO[respondent]
  df$c_size[row+23] <- d$size24_DO[respondent]
  df$c_rural[row+23] <- d$rural24_DO[respondent]
  df$c_comp[row+23] <- d$comp24_DO[respondent]
  df$c_pol[row+23] <- d$pol24_DO[respondent]
  df$c_bs[row+23] <- d$bs24_DO[respondent]
  df$c_rate[row+23] <- d$offer2_12[respondent]
  df$c_choose[row+23] <- 3-d$accept_12[respondent]  
  
  #Trial 13
  df$c_salary[row+24] <- d$sal25_DO[respondent]
  df$c_size[row+24] <- d$size25_DO[respondent]
  df$c_rural[row+24] <- d$rural25_DO[respondent]
  df$c_comp[row+24] <- d$comp25_DO[respondent]
  df$c_pol[row+24] <- d$pol25_DO[respondent]
  df$c_bs[row+24] <- d$bs25_DO[respondent]
  df$c_rate[row+24] <- d$offer1_13[respondent]
  df$c_choose[row+24] <- d$accept_13[respondent]
  
  df$c_salary[row+25] <- d$sal26_DO[respondent]
  df$c_size[row+25] <- d$size26_DO[respondent]
  df$c_rural[row+25] <- d$rural26_DO[respondent]
  df$c_comp[row+25] <- d$comp26_DO[respondent]
  df$c_pol[row+25] <- d$pol26_DO[respondent]
  df$c_bs[row+25] <- d$bs26_DO[respondent]
  df$c_rate[row+25] <- d$offer2_13[respondent]
  df$c_choose[row+25] <- 3-d$accept_13[respondent]  
  
  #Trial 14
  df$c_salary[row+26] <- d$sal27_DO[respondent]
  df$c_size[row+26] <- d$size27_DO[respondent]
  df$c_rural[row+26] <- d$rural27_DO[respondent]
  df$c_comp[row+26] <- d$comp27_DO[respondent]
  df$c_pol[row+26] <- d$pol27_DO[respondent]
  df$c_bs[row+26] <- d$bs27_DO[respondent]
  df$c_rate[row+26] <- d$offer1_14[respondent]
  df$c_choose[row+26] <- d$accept_14[respondent]
  
  df$c_salary[row+27] <- d$sal28_DO[respondent]
  df$c_size[row+27] <- d$size28_DO[respondent]
  df$c_rural[row+27] <- d$rural28_DO[respondent]
  df$c_comp[row+27] <- d$comp28_DO[respondent]
  df$c_pol[row+27] <- d$pol28_DO[respondent]
  df$c_bs[row+27] <- d$bs28_DO[respondent]
  df$c_rate[row+27] <- d$offer2_14[respondent]
  df$c_choose[row+27] <- 3-d$accept_14[respondent]  
  
  #Trial 15
  df$c_salary[row+28] <- d$sal29_DO[respondent]
  df$c_size[row+28] <- d$size29_DO[respondent]
  df$c_rural[row+28] <- d$rural29_DO[respondent]
  df$c_comp[row+28] <- d$comp29_DO[respondent]
  df$c_pol[row+28] <- d$pol29_DO[respondent]
  df$c_bs[row+28] <- d$bs29_DO[respondent]
  df$c_rate[row+28] <- d$offer1_15[respondent]
  df$c_choose[row+28] <- d$accept_15[respondent]
  
  df$c_salary[row+29] <- d$sal30_DO[respondent]
  df$c_size[row+29] <- d$size30_DO[respondent]
  df$c_rural[row+29] <- d$rural30_DO[respondent]
  df$c_comp[row+29] <- d$comp30_DO[respondent]
  df$c_pol[row+29] <- d$pol30_DO[respondent]
  df$c_bs[row+29] <- d$bs30_DO[respondent]
  df$c_rate[row+29] <- d$offer2_15[respondent]
  df$c_choose[row+29] <- 3-d$accept_5[respondent]  
  
  row <- row+30
}

df$c_choose <- recode(df$c_choose,"2=0")
df$c_salary <- factor(recode(df$c_salary,"1='1_75k';2='2_90k';3='3-105k'"))
df$c_size <- factor(recode(df$c_size,"1='1_10Employees';2='2-2500Employees';3='3_500000Employees'"))
df$c_rural <- factor(recode(df$c_rural,"1='Rural';2='CollegeTown';3='Midsize';4='Metro'"))
df$c_comp <- factor(recode(df$c_comp,"1='Variety';2='Feedback';3='Advancement';4='Talent'"))
df$c_pol <- factor(recode(df$c_pol,"1='1_ClintonStrong';2='2_ClintonWeak';3='3_TrumpWeak';4='4_TrumpStrong'"))
df$c_bs <- factor(recode(df$c_bs,"1='VoterID';2='Unionize';3='Protest';4='Governor';5='Redistricting';6='BikeTrails';7='Corruption'"))
df$c_rate <- recode(df$c_rate,"1=5;2=4;5=3;3=2;4=1")

table(df$trial)
table(df$c_salary)
table(df$c_size)
table(df$c_rural)
table(df$c_comp)
table(df$c_pol)
table(df$c_bs)
prop.table(table(df$c_rate))
prop.table(table(df$c_choose))
prop.table(table(student$pid))

df$Salary <- factor(df$c_salary,levels=c("1_75k", "2_90k", "3-105k"),labels=c("$75,000","$90,000","$105,000"))
df$Size <- factor(df$c_size,levels=c("1_10Employees", "2-2500Employees", "3_500000Employees"),labels=c("10 Employees","2,500 Employees","500,000 Employees"))
df$Location <- factor(df$c_rural,levels=c("CollegeTown","Metro","Midsize","Rural"),labels=c("College Town","Metro Area","Midsize City","Rural Area"))
df$Culture <- factor(df$c_comp,levels=c("Advancement","Feedback", "Talent","Variety"),labels=c("Advancement Opportunity","Frequent Feedback", "Great Talent","Task Variety"))
df$Partisanship <- factor(df$c_pol,levels=c("1_ClintonStrong","2_ClintonWeak","3_TrumpWeak","4_TrumpStrong"),labels=c("Strong Clinton State","Weak Clinton State","Weak Trump State","Strong Trump State"))
df$Backsliding <- factor(df$c_bs,levels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","VoterID"),labels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","VoterID"))
df$Party <- recode(df$pid, "1:3=0;4=1;5:7=2")
df$Party <- factor(df$Party,levels=c(0,1,2),labels=c("Democrat","Independent","Republican"))

df1 <- na.omit(df)

###CREATE MTURK DATASET
mturk <- df1
write.csv(mturk,"NelsonWitko_JOP_mturk.csv")


###PREPARE STUDENT DATA
d <-  read.csv("DemBacksliding_Student.csv")
dim(d)

d$pid <-NA
d$pid[d$PID2==1] <- 1
d$pid[d$PID2==2] <- 2
d$pid[d$PID4==2] <- 3
d$pid[d$PID4==3] <- 4
d$pid[d$PID4==1] <- 5
d$pid[d$PID3==2] <- 6
d$pid[d$PID3==1] <- 7
table(d$pid)

#Table A1 [MTurk]
prop.table(table((d$GENDER)))
prop.table(table((d$RACE)))
prop.table(table((d$HISP)))
d$age <- 2019-d$YRBORN
mean(d$age, na.rm=T)
mean(d$IDEO, na.rm=T)

df <- setNames(data.frame(matrix(ncol = 12, nrow = dim(d)[1]*30)), c("responseId","respondent","trial","c_salary","c_size", "c_rural", "c_comp", "c_pol", "c_bs","c_rate","c_choose","pid"))
d$ResponseId <- as.character(d$ResponseId)
respondent <- 1
row <- 1
for (respondent in 1:dim(d)[1]){
  df$responseId[row] <- d$ResponseId[respondent]
  df$responseId[row+1] <- d$ResponseId[respondent]
  df$responseId[row+2] <- d$ResponseId[respondent]
  df$responseId[row+3] <- d$ResponseId[respondent]
  df$responseId[row+4] <- d$ResponseId[respondent]
  df$responseId[row+5] <- d$ResponseId[respondent]
  df$responseId[row+6] <- d$ResponseId[respondent]
  df$responseId[row+7] <- d$ResponseId[respondent]
  df$responseId[row+8] <- d$ResponseId[respondent]
  df$responseId[row+9] <- d$ResponseId[respondent]
  df$responseId[row+10] <- d$ResponseId[respondent]
  df$responseId[row+11] <- d$ResponseId[respondent]
  df$responseId[row+12] <- d$ResponseId[respondent]
  df$responseId[row+13] <- d$ResponseId[respondent]
  df$responseId[row+14] <- d$ResponseId[respondent]
  df$responseId[row+15] <- d$ResponseId[respondent]
  df$responseId[row+16] <- d$ResponseId[respondent]
  df$responseId[row+17] <- d$ResponseId[respondent]
  df$responseId[row+18] <- d$ResponseId[respondent]
  df$responseId[row+19] <- d$ResponseId[respondent]
  df$responseId[row+20] <- d$ResponseId[respondent]
  df$responseId[row+21] <- d$ResponseId[respondent]
  df$responseId[row+22] <- d$ResponseId[respondent]
  df$responseId[row+23] <- d$ResponseId[respondent]
  df$responseId[row+24] <- d$ResponseId[respondent]
  df$responseId[row+25] <- d$ResponseId[respondent]
  df$responseId[row+26] <- d$ResponseId[respondent]
  df$responseId[row+27] <- d$ResponseId[respondent]
  df$responseId[row+28] <- d$ResponseId[respondent]
  df$responseId[row+29] <- d$ResponseId[respondent]
  df$respondent[row] <- respondent
  df$respondent[row+1] <- respondent
  df$respondent[row+2] <- respondent
  df$respondent[row+3] <- respondent
  df$respondent[row+4] <- respondent
  df$respondent[row+5] <- respondent
  df$respondent[row+6] <- respondent
  df$respondent[row+7] <- respondent
  df$respondent[row+8] <- respondent
  df$respondent[row+9] <- respondent
  df$respondent[row+10] <- respondent
  df$respondent[row+11] <- respondent
  df$respondent[row+12] <- respondent
  df$respondent[row+13] <- respondent
  df$respondent[row+14] <- respondent
  df$respondent[row+15] <- respondent
  df$respondent[row+16] <- respondent
  df$respondent[row+17] <- respondent
  df$respondent[row+18] <- respondent
  df$respondent[row+19] <- respondent
  df$respondent[row+20] <- respondent
  df$respondent[row+21] <- respondent
  df$respondent[row+22] <- respondent
  df$respondent[row+23] <- respondent
  df$respondent[row+24] <- respondent
  df$respondent[row+25] <- respondent
  df$respondent[row+26] <- respondent
  df$respondent[row+27] <- respondent
  df$respondent[row+28] <- respondent
  df$respondent[row+29] <- respondent
  df$trial[row] <- 1
  df$trial[row+1] <- 1
  df$trial[row+2] <- 2
  df$trial[row+3] <- 2
  df$trial[row+4] <- 3
  df$trial[row+5] <- 3
  df$trial[row+6] <- 4
  df$trial[row+7] <- 4
  df$trial[row+8] <- 5
  df$trial[row+9] <- 5
  df$trial[row+10] <- 6
  df$trial[row+11] <- 6
  df$trial[row+12] <- 7
  df$trial[row+13] <- 7
  df$trial[row+14] <- 8
  df$trial[row+15] <- 8
  df$trial[row+16] <- 9
  df$trial[row+17] <- 9
  df$trial[row+18] <- 10
  df$trial[row+19] <- 10
  df$trial[row+20] <- 11
  df$trial[row+21] <- 11
  df$trial[row+22] <- 12
  df$trial[row+23] <- 12
  df$trial[row+24] <- 13
  df$trial[row+25] <- 13
  df$trial[row+26] <- 14
  df$trial[row+27] <- 14
  df$trial[row+28] <- 15
  df$trial[row+29] <- 15
  df$c_number[row] <- 1
  df$c_number[row+1] <- 2
  df$c_number[row+2] <- 1
  df$c_number[row+3] <- 2
  df$c_number[row+4] <- 1
  df$c_number[row+5] <- 2
  df$c_number[row+6] <- 1
  df$c_number[row+7] <- 2
  df$c_number[row+8] <- 1
  df$c_number[row+9] <- 2
  df$c_number[row+10] <- 1
  df$c_number[row+11] <- 2
  df$c_number[row+12] <- 1
  df$c_number[row+13] <- 2
  df$c_number[row+14] <- 1
  df$c_number[row+15] <- 2
  df$c_number[row+16] <- 1
  df$c_number[row+17] <- 2
  df$c_number[row+18] <- 1
  df$c_number[row+19] <- 2
  df$c_number[row+20] <- 1
  df$c_number[row+21] <- 2
  df$c_number[row+22] <- 1
  df$c_number[row+23] <- 2
  df$c_number[row+24] <- 1
  df$c_number[row+25] <- 2
  df$c_number[row+26] <- 1
  df$c_number[row+27] <- 2
  df$c_number[row+28] <- 1
  df$c_number[row+29] <- 2
  
  df$pid[row] <- d$pid[respondent]
  df$pid[row+1] <- d$pid[respondent]
  df$pid[row+2] <- d$pid[respondent]
  df$pid[row+3] <- d$pid[respondent]
  df$pid[row+4] <- d$pid[respondent]
  df$pid[row+5] <- d$pid[respondent]
  df$pid[row+6] <- d$pid[respondent]
  df$pid[row+7] <- d$pid[respondent]
  df$pid[row+8] <- d$pid[respondent]
  df$pid[row+9] <- d$pid[respondent]
  df$pid[row+10] <- d$pid[respondent]
  df$pid[row+11] <- d$pid[respondent]
  df$pid[row+12] <- d$pid[respondent]
  df$pid[row+13] <- d$pid[respondent]
  df$pid[row+14] <- d$pid[respondent]
  df$pid[row+15] <- d$pid[respondent]
  df$pid[row+16] <- d$pid[respondent]
  df$pid[row+17] <- d$pid[respondent]
  df$pid[row+18] <- d$pid[respondent]
  df$pid[row+19] <- d$pid[respondent]
  df$pid[row+20] <- d$pid[respondent]
  df$pid[row+21] <- d$pid[respondent]
  df$pid[row+22] <- d$pid[respondent]
  df$pid[row+23] <- d$pid[respondent]
  df$pid[row+24] <- d$pid[respondent]
  df$pid[row+25] <- d$pid[respondent]
  df$pid[row+26] <- d$pid[respondent]
  df$pid[row+27] <- d$pid[respondent]
  df$pid[row+28] <- d$pid[respondent]
  df$pid[row+29] <- d$pid[respondent]
  
  #Trial 1
  df$c_salary[row] <- d$sal1_DO[respondent]
  df$c_size[row] <- d$size1_DO[respondent]
  df$c_rural[row] <- d$rural1_DO[respondent]
  df$c_comp[row] <- d$comp1_DO[respondent]
  df$c_pol[row] <- d$pol1_DO[respondent]
  df$c_bs[row] <- d$bs1_DO[respondent]
  df$c_rate[row] <- d$offer1_1[respondent]
  df$c_choose[row] <- d$accept_1[respondent]
  
  df$c_salary[row+1] <- d$sal2_DO[respondent]
  df$c_size[row+1] <- d$size2_DO[respondent]
  df$c_rural[row+1] <- d$rural2_DO[respondent]
  df$c_comp[row+1] <- d$comp2_DO[respondent]
  df$c_pol[row+1] <- d$pol2_DO[respondent]
  df$c_bs[row+1] <- d$bs2_DO[respondent]
  df$c_rate[row+1] <- d$offer2_1[respondent]
  df$c_choose[row+1] <- 3-d$accept_1[respondent]  
  
  #Trial 2
  df$c_salary[row+2] <- d$sal3_DO[respondent]
  df$c_size[row+2] <- d$size3_DO[respondent]
  df$c_rural[row+2] <- d$rural3_DO[respondent]
  df$c_comp[row+2] <- d$comp3_DO[respondent]
  df$c_pol[row+2] <- d$pol3_DO[respondent]
  df$c_bs[row+2] <- d$bs3_DO[respondent]
  df$c_rate[row+2] <- d$offer1_2[respondent]
  df$c_choose[row+2] <- d$accept_2[respondent]
  
  df$c_salary[row+3] <- d$sal4_DO[respondent]
  df$c_size[row+3] <- d$size4_DO[respondent]
  df$c_rural[row+3] <- d$rural4_DO[respondent]
  df$c_comp[row+3] <- d$comp4_DO[respondent]
  df$c_pol[row+3] <- d$pol4_DO[respondent]
  df$c_bs[row+3] <- d$bs4_DO[respondent]
  df$c_rate[row+3] <- d$offer2_2[respondent]
  df$c_choose[row+3] <- 3-d$accept_2[respondent]  
  
  #Trial 3
  df$c_salary[row+4] <- d$sal5_DO[respondent]
  df$c_size[row+4] <- d$size5_DO[respondent]
  df$c_rural[row+4] <- d$rural5_DO[respondent]
  df$c_comp[row+4] <- d$comp5_DO[respondent]
  df$c_pol[row+4] <- d$pol5_DO[respondent]
  df$c_bs[row+4] <- d$bs5_DO[respondent]
  df$c_rate[row+4] <- d$offer1_3[respondent]
  df$c_choose[row+4] <- d$accept_3[respondent]
  
  df$c_salary[row+5] <- d$sal6_DO[respondent]
  df$c_size[row+5] <- d$size6_DO[respondent]
  df$c_rural[row+5] <- d$rural6_DO[respondent]
  df$c_comp[row+5] <- d$comp6_DO[respondent]
  df$c_pol[row+5] <- d$pol6_DO[respondent]
  df$c_bs[row+5] <- d$bs6_DO[respondent]
  df$c_rate[row+5] <- d$offer2_3[respondent]
  df$c_choose[row+5] <- 3-d$accept_3[respondent]  
  
  #Trial 4
  df$c_salary[row+6] <- d$sal7_DO[respondent]
  df$c_size[row+6] <- d$size7_DO[respondent]
  df$c_rural[row+6] <- d$rural7_DO[respondent]
  df$c_comp[row+6] <- d$comp7_DO[respondent]
  df$c_pol[row+6] <- d$pol7_DO[respondent]
  df$c_bs[row+6] <- d$bs7_DO[respondent]
  df$c_rate[row+6] <- d$offer1_4[respondent]
  df$c_choose[row+6] <- d$accept_4[respondent]
  
  df$c_salary[row+7] <- d$sal8_DO[respondent]
  df$c_size[row+7] <- d$size8_DO[respondent]
  df$c_rural[row+7] <- d$rural8_DO[respondent]
  df$c_comp[row+7] <- d$comp8_DO[respondent]
  df$c_pol[row+7] <- d$pol8_DO[respondent]
  df$c_bs[row+7] <- d$bs8_DO[respondent]
  df$c_rate[row+7] <- d$offer2_4[respondent]
  df$c_choose[row+7] <- 3-d$accept_4[respondent]  
  
  #Trial 5
  df$c_salary[row+8] <- d$sal9_DO[respondent]
  df$c_size[row+8] <- d$size9_DO[respondent]
  df$c_rural[row+8] <- d$rural9_DO[respondent]
  df$c_comp[row+8] <- d$comp9_DO[respondent]
  df$c_pol[row+8] <- d$pol9_DO[respondent]
  df$c_bs[row+8] <- d$bs9_DO[respondent]
  df$c_rate[row+8] <- d$offer1_5[respondent]
  df$c_choose[row+8] <- d$accept_9[respondent]
  
  df$c_salary[row+9] <- d$sal10_DO[respondent]
  df$c_size[row+9] <- d$size10_DO[respondent]
  df$c_rural[row+9] <- d$rural10_DO[respondent]
  df$c_comp[row+9] <- d$comp10_DO[respondent]
  df$c_pol[row+9] <- d$pol10_DO[respondent]
  df$c_bs[row+9] <- d$bs10_DO[respondent]
  df$c_rate[row+9] <- d$offer2_5[respondent]
  df$c_choose[row+9] <- 3-d$accept_5[respondent]  
  
  #Trial 6
  df$c_salary[row+10] <- d$sal11_DO[respondent]
  df$c_size[row+10] <- d$size11_DO[respondent]
  df$c_rural[row+10] <- d$rural11_DO[respondent]
  df$c_comp[row+10] <- d$comp11_DO[respondent]
  df$c_pol[row+10] <- d$pol11_DO[respondent]
  df$c_bs[row+10] <- d$bs11_DO[respondent]
  df$c_rate[row+10] <- d$offer1_6[respondent]
  df$c_choose[row+10] <- d$accept_6[respondent]
  
  df$c_salary[row+11] <- d$sal12_DO[respondent]
  df$c_size[row+11] <- d$size12_DO[respondent]
  df$c_rural[row+11] <- d$rural12_DO[respondent]
  df$c_comp[row+11] <- d$comp12_DO[respondent]
  df$c_pol[row+11] <- d$pol12_DO[respondent]
  df$c_bs[row+11] <- d$bs12_DO[respondent]
  df$c_rate[row+11] <- d$offer2_6[respondent]
  df$c_choose[row+11] <- 3-d$accept_6[respondent]  
  
  #Trial 7
  df$c_salary[row+12] <- d$sal13_DO[respondent]
  df$c_size[row+12] <- d$size13_DO[respondent]
  df$c_rural[row+12] <- d$rural13_DO[respondent]
  df$c_comp[row+12] <- d$comp13_DO[respondent]
  df$c_pol[row+12] <- d$pol13_DO[respondent]
  df$c_bs[row+12] <- d$bs13_DO[respondent]
  df$c_rate[row+12] <- d$offer1_7[respondent]
  df$c_choose[row+12] <- d$accept_7[respondent]
  
  df$c_salary[row+13] <- d$sal14_DO[respondent]
  df$c_size[row+13] <- d$size14_DO[respondent]
  df$c_rural[row+13] <- d$rural14_DO[respondent]
  df$c_comp[row+13] <- d$comp14_DO[respondent]
  df$c_pol[row+13] <- d$pol14_DO[respondent]
  df$c_bs[row+13] <- d$bs14_DO[respondent]
  df$c_rate[row+13] <- d$offer2_7[respondent]
  df$c_choose[row+13] <- 3-d$accept_7[respondent]  
  
  #Trial 8
  df$c_salary[row+14] <- d$sal15_DO[respondent]
  df$c_size[row+14] <- d$size15_DO[respondent]
  df$c_rural[row+14] <- d$rural15_DO[respondent]
  df$c_comp[row+14] <- d$comp15_DO[respondent]
  df$c_pol[row+14] <- d$pol15_DO[respondent]
  df$c_bs[row+14] <- d$bs15_DO[respondent]
  df$c_rate[row+14] <- d$offer1_8[respondent]
  df$c_choose[row+14] <- d$accept_8[respondent]
  
  df$c_salary[row+15] <- d$sal16_DO[respondent]
  df$c_size[row+15] <- d$size16_DO[respondent]
  df$c_rural[row+15] <- d$rural16_DO[respondent]
  df$c_comp[row+15] <- d$comp16_DO[respondent]
  df$c_pol[row+15] <- d$pol16_DO[respondent]
  df$c_bs[row+15] <- d$bs16_DO[respondent]
  df$c_rate[row+15] <- d$offer2_8[respondent]
  df$c_choose[row+15] <- 3-d$accept_8[respondent]  
  
  #Trial 9
  df$c_salary[row+16] <- d$sal17_DO[respondent]
  df$c_size[row+16] <- d$size17_DO[respondent]
  df$c_rural[row+16] <- d$rural17_DO[respondent]
  df$c_comp[row+16] <- d$comp17_DO[respondent]
  df$c_pol[row+16] <- d$pol17_DO[respondent]
  df$c_bs[row+16] <- d$bs17_DO[respondent]
  df$c_rate[row+16] <- d$offer1_9[respondent]
  df$c_choose[row+16] <- d$accept_9[respondent]
  
  df$c_salary[row+17] <- d$sal18_DO[respondent]
  df$c_size[row+17] <- d$size18_DO[respondent]
  df$c_rural[row+17] <- d$rural18_DO[respondent]
  df$c_comp[row+17] <- d$comp18_DO[respondent]
  df$c_pol[row+17] <- d$pol18_DO[respondent]
  df$c_bs[row+17] <- d$bs18_DO[respondent]
  df$c_rate[row+17] <- d$offer2_9[respondent]
  df$c_choose[row+17] <- 3-d$accept_9[respondent]  
  
  #Trial 10
  df$c_salary[row+18] <- d$sal19_DO[respondent]
  df$c_size[row+18] <- d$size19_DO[respondent]
  df$c_rural[row+18] <- d$rural19_DO[respondent]
  df$c_comp[row+18] <- d$comp19_DO[respondent]
  df$c_pol[row+18] <- d$pol19_DO[respondent]
  df$c_bs[row+18] <- d$bs19_DO[respondent]
  df$c_rate[row+18] <- d$offer1_10[respondent]
  df$c_choose[row+18] <- d$accept_10[respondent]
  
  df$c_salary[row+19] <- d$sal20_DO[respondent]
  df$c_size[row+19] <- d$size20_DO[respondent]
  df$c_rural[row+19] <- d$rural20_DO[respondent]
  df$c_comp[row+19] <- d$comp20_DO[respondent]
  df$c_pol[row+19] <- d$pol20_DO[respondent]
  df$c_bs[row+19] <- d$bs20_DO[respondent]
  df$c_rate[row+19] <- d$offer2_10[respondent]
  df$c_choose[row+19] <- 3-d$accept_10[respondent]  
  
  #Trial 11
  df$c_salary[row+20] <- d$sal21_DO[respondent]
  df$c_size[row+20] <- d$size21_DO[respondent]
  df$c_rural[row+20] <- d$rural21_DO[respondent]
  df$c_comp[row+20] <- d$comp21_DO[respondent]
  df$c_pol[row+20] <- d$pol21_DO[respondent]
  df$c_bs[row+20] <- d$bs21_DO[respondent]
  df$c_rate[row+20] <- d$offer1_11[respondent]
  df$c_choose[row+20] <- d$accept_11[respondent]
  
  df$c_salary[row+21] <- d$sal22_DO[respondent]
  df$c_size[row+21] <- d$size22_DO[respondent]
  df$c_rural[row+21] <- d$rural22_DO[respondent]
  df$c_comp[row+21] <- d$comp22_DO[respondent]
  df$c_pol[row+21] <- d$pol2_DO[respondent]
  df$c_bs[row+21] <- d$bs22_DO[respondent]
  df$c_rate[row+21] <- d$offer2_11[respondent]
  df$c_choose[row+21] <- 3-d$accept_11[respondent]    
  
  #Trial 12
  df$c_salary[row+22] <- d$sal23_DO[respondent]
  df$c_size[row+22] <- d$size23_DO[respondent]
  df$c_rural[row+22] <- d$rural23_DO[respondent]
  df$c_comp[row+22] <- d$comp23_DO[respondent]
  df$c_pol[row+22] <- d$pol23_DO[respondent]
  df$c_bs[row+22] <- d$bs23_DO[respondent]
  df$c_rate[row+22] <- d$offer1_12[respondent]
  df$c_choose[row+22] <- d$accept_12[respondent]
  
  df$c_salary[row+23] <- d$sal24_DO[respondent]
  df$c_size[row+23] <- d$size24_DO[respondent]
  df$c_rural[row+23] <- d$rural24_DO[respondent]
  df$c_comp[row+23] <- d$comp24_DO[respondent]
  df$c_pol[row+23] <- d$pol24_DO[respondent]
  df$c_bs[row+23] <- d$bs24_DO[respondent]
  df$c_rate[row+23] <- d$offer2_12[respondent]
  df$c_choose[row+23] <- 3-d$accept_12[respondent]  
  
  #Trial 13
  df$c_salary[row+24] <- d$sal25_DO[respondent]
  df$c_size[row+24] <- d$size25_DO[respondent]
  df$c_rural[row+24] <- d$rural25_DO[respondent]
  df$c_comp[row+24] <- d$comp25_DO[respondent]
  df$c_pol[row+24] <- d$pol25_DO[respondent]
  df$c_bs[row+24] <- d$bs25_DO[respondent]
  df$c_rate[row+24] <- d$offer1_13[respondent]
  df$c_choose[row+24] <- d$accept_13[respondent]
  
  df$c_salary[row+25] <- d$sal26_DO[respondent]
  df$c_size[row+25] <- d$size26_DO[respondent]
  df$c_rural[row+25] <- d$rural26_DO[respondent]
  df$c_comp[row+25] <- d$comp26_DO[respondent]
  df$c_pol[row+25] <- d$pol26_DO[respondent]
  df$c_bs[row+25] <- d$bs26_DO[respondent]
  df$c_rate[row+25] <- d$offer2_13[respondent]
  df$c_choose[row+25] <- 3-d$accept_13[respondent]  
  
  #Trial 14
  df$c_salary[row+26] <- d$sal27_DO[respondent]
  df$c_size[row+26] <- d$size27_DO[respondent]
  df$c_rural[row+26] <- d$rural27_DO[respondent]
  df$c_comp[row+26] <- d$comp27_DO[respondent]
  df$c_pol[row+26] <- d$pol27_DO[respondent]
  df$c_bs[row+26] <- d$bs27_DO[respondent]
  df$c_rate[row+26] <- d$offer1_14[respondent]
  df$c_choose[row+26] <- d$accept_14[respondent]
  
  df$c_salary[row+27] <- d$sal28_DO[respondent]
  df$c_size[row+27] <- d$size28_DO[respondent]
  df$c_rural[row+27] <- d$rural28_DO[respondent]
  df$c_comp[row+27] <- d$comp28_DO[respondent]
  df$c_pol[row+27] <- d$pol28_DO[respondent]
  df$c_bs[row+27] <- d$bs28_DO[respondent]
  df$c_rate[row+27] <- d$offer2_14[respondent]
  df$c_choose[row+27] <- 3-d$accept_14[respondent]  
  
  #Trial 15
  df$c_salary[row+28] <- d$sal29_DO[respondent]
  df$c_size[row+28] <- d$size29_DO[respondent]
  df$c_rural[row+28] <- d$rural29_DO[respondent]
  df$c_comp[row+28] <- d$comp29_DO[respondent]
  df$c_pol[row+28] <- d$pol29_DO[respondent]
  df$c_bs[row+28] <- d$bs29_DO[respondent]
  df$c_rate[row+28] <- d$offer1_15[respondent]
  df$c_choose[row+28] <- d$accept_15[respondent]
  
  df$c_salary[row+29] <- d$sal30_DO[respondent]
  df$c_size[row+29] <- d$size30_DO[respondent]
  df$c_rural[row+29] <- d$rural30_DO[respondent]
  df$c_comp[row+29] <- d$comp30_DO[respondent]
  df$c_pol[row+29] <- d$pol30_DO[respondent]
  df$c_bs[row+29] <- d$bs30_DO[respondent]
  df$c_rate[row+29] <- d$offer2_15[respondent]
  df$c_choose[row+29] <- 3-d$accept_5[respondent]  
  
  row <- row+30
}

df$c_choose <- recode(df$c_choose,"2=0")
df$c_salary <- factor(recode(df$c_salary,"1='1_75k';2='2_90k';3='3-105k'"))
df$c_size <- factor(recode(df$c_size,"1='1_10Employees';2='2-2500Employees';3='3_500000Employees'"))
df$c_rural <- factor(recode(df$c_rural,"1='Rural';2='CollegeTown';3='Midsize';4='Metro'"))
df$c_comp <- factor(recode(df$c_comp,"1='Variety';2='Feedback';3='Advancement';4='Talent'"))
df$c_pol <- factor(recode(df$c_pol,"1='1_ClintonStrong';2='2_ClintonWeak';3='3_TrumpWeak';4='4_TrumpStrong'"))
df$c_bs <- factor(recode(df$c_bs,"1='VoterID';2='Unionize';3='Protest';4='Governor';5='Redistricting';6='BikeTrails';7='Corruption'"))
df$c_rate <- recode(df$c_rate,"1=5;2=4;5=3;3=2;4=1")

table(df$trial)
table(df$c_salary)
table(df$c_size)
table(df$c_rural)
table(df$c_comp)
table(df$c_pol)
table(df$c_bs)
prop.table(table(df$c_rate))
prop.table(table(df$c_choose))
prop.table(table(student$pid))

df$Salary <- factor(df$c_salary,levels=c("1_75k", "2_90k", "3-105k"),labels=c("$75,000","$90,000","$105,000"))
df$Size <- factor(df$c_size,levels=c("1_10Employees", "2-2500Employees", "3_500000Employees"),labels=c("10 Employees","2,500 Employees","500,000 Employees"))
df$Location <- factor(df$c_rural,levels=c("CollegeTown","Metro","Midsize","Rural"),labels=c("College Town","Metro Area","Midsize City","Rural Area"))
df$Culture <- factor(df$c_comp,levels=c("Advancement","Feedback", "Talent","Variety"),labels=c("Advancement Opportunity","Frequent Feedback", "Great Talent","Task Variety"))
df$Partisanship <- factor(df$c_pol,levels=c("1_ClintonStrong","2_ClintonWeak","3_TrumpWeak","4_TrumpStrong"),labels=c("Strong Clinton State","Weak Clinton State","Weak Trump State","Strong Trump State"))
df$Backsliding <- factor(df$c_bs,levels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","VoterID"),labels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","VoterID"))
df$Party <- recode(df$pid, "1:3=0;4=1;5:7=2")
df$Party <- factor(df$Party,levels=c(0,1,2),labels=c("Democrat","Independent","Republican"))

df1 <- na.omit(df)

#####CREATE STUDENT DATASET
student <- df1
write.csv(student,"NelsonWitko_JOP_student.csv")


##ANALYZE DATA
mturk <- read.csv("NelsonWitko_JOP_mturk.csv")
student <- read.csv("NelsonWitko_JOP_student.csv")

##Set Baselines
mturk$Salary <- factor(mturk$c_salary,levels=c("1_75k", "2_90k", "3-105k"),labels=c("$75,000","$90,000","$105,000"))
mturk$Size <- factor(mturk$c_size,levels=c("1_10Employees", "2-2500Employees", "3_500000Employees"),labels=c("10 Employees","2,500 Employees","500,000 Employees"))
mturk$Location <- factor(mturk$c_rural,levels=c("CollegeTown","Metro","Midsize","Rural"),labels=c("College Town","Metro Area","Midsize City","Rural Area"))
mturk$Culture <- factor(mturk$c_comp,levels=c("Advancement","Feedback", "Talent","Variety"),labels=c("Advancement Opportunity","Frequent Feedback", "Great Talent","Task Variety"))
mturk$Partisanship <- factor(mturk$c_pol,levels=c("1_ClintonStrong","2_ClintonWeak","3_TrumpWeak","4_TrumpStrong"),labels=c("Strong Clinton State","Weak Clinton State","Weak Trump State","Strong Trump State"))
mturk$Backsliding <- factor(mturk$c_bs,levels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","VoterID"),labels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","Voter ID"))
mturk$Party <- recode(mturk$pid, "1:3=0;4=1;5:7=2")
mturk$Party <- factor(mturk$Party,levels=c(0,1,2),labels=c("Democrat","Independent","Republican"))

student$Salary <- factor(student$c_salary,levels=c("1_75k", "2_90k", "3-105k"),labels=c("$75,000","$90,000","$105,000"))
student$Size <- factor(student$c_size,levels=c("1_10Employees", "2-2500Employees", "3_500000Employees"),labels=c("10 Employees","2,500 Employees","500,000 Employees"))
student$Location <- factor(student$c_rural,levels=c("CollegeTown","Metro","Midsize","Rural"),labels=c("College Town","Metro Area","Midsize City","Rural Area"))
student$Culture <- factor(student$c_comp,levels=c("Advancement","Feedback", "Talent","Variety"),labels=c("Advancement Opportunity","Frequent Feedback", "Great Talent","Task Variety"))
student$Partisanship <- factor(student$c_pol,levels=c("1_ClintonStrong","2_ClintonWeak","3_TrumpWeak","4_TrumpStrong"),labels=c("Strong Clinton State","Weak Clinton State","Weak Trump State","Strong Trump State"))
student$Backsliding <- factor(student$c_bs,levels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","VoterID"),labels=c("BikeTrails","Corruption","Governor","Protest","Redistricting","Unionize","Voter ID"))
student$Party <- recode(student$pid, "1:3=0;4=1;5:7=2")
student$Party <- factor(student$Party,levels=c(0,1,2),labels=c("Democrat","Independent","Republican"))


###########################
####Unconditional AMCEs####
###########################

##Rating DV (Estimates for Tables A2 and A3; Make Figures 1 (left-hand panel) and A2)
studentresults <- amce(c_rate~Salary+Size+Location+Culture+Partisanship+Backsliding, data=student,
                       cluster=TRUE, respondent.id="respondent")
mturkresults <- amce(c_rate~Salary+Size+Location+Culture+Partisanship+Backsliding, data=mturk,
                     cluster=TRUE, respondent.id="respondent")
summary(studentresults)
summary(mturkresults)
attribute <-as.vector(summary(mturkresults)$amce[1])$Attribute
level <- as.vector(summary(mturkresults)$amce[2])$Level
student_acme <- as.vector(summary(studentresults)$amce[3])$Estimate
student_se <- as.vector(summary(studentresults)$amce[4])$`Std. Err`
mturk_acme <- as.vector(summary(mturkresults)$amce[3])$Estimate
mturk_se <- as.vector(summary(mturkresults)$amce[4])$`Std. Err`


results_df <- data.frame(attribute = rep(attribute, times = 2),
                         term = rep(level, times = 2),
                         estimate = c(mturk_acme,student_acme),
                         std.error = c(mturk_se,student_se),
                         model = c(rep("MTurk Sample", 19), rep("Student Sample", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() + 
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                              c("Culture", "Frequent Feedback", "Task Variety"),
                              c("Location", "Metro Area", "Rural Area"),
                              c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                              c("Salary", "$90,000", "$105,000"),
                              c("Size", "2,500 Employees", "500,000 Employees")))
p1

##Make Figure A2
pdf("rr_rate.pdf",width=12,height=12)
print(p1)
dev.off()

#Only Backsliding (Figure 1, left panel)
results_df1 <- subset(results_df,attribute=="Backsliding")
pdf("rr_rate_backsliding.pdf",width=8,height=4)
dwplot(results_df1,dot_args = list(aes(shape = model))) +
  theme_bw() + 
  theme(legend.justification=c(0,0), legend.position=c(0,0),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14),
        plot.title = element_text(hjust = 0.5)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("Job Rating Outcome")
dev.off()

##Choose DV (Estimates for Tables A6 and A7; Make Figures 1 (right-hand panel) and A6)
studentresults <- amce(c_choose~Salary+Size+Location+Culture+Partisanship+Backsliding, data=student,
                       cluster=TRUE, respondent.id="respondent")
mturkresults <- amce(c_choose~Salary+Size+Location+Culture+Partisanship+Backsliding, data=mturk,
                     cluster=TRUE, respondent.id="respondent")
summary(studentresults)
summary(mturkresults)
attribute <-as.vector(summary(mturkresults)$amce[1])$Attribute
level <- as.vector(summary(mturkresults)$amce[2])$Level
student_acme <- as.vector(summary(studentresults)$amce[3])$Estimate
student_se <- as.vector(summary(studentresults)$amce[4])$`Std. Err`
mturk_acme <- as.vector(summary(mturkresults)$amce[3])$Estimate
mturk_se <- as.vector(summary(mturkresults)$amce[4])$`Std. Err`


results_df <- data.frame(attribute = rep(attribute, times = 2),
                         term = rep(level, times = 2),
                         estimate = c(mturk_acme,student_acme),
                         std.error = c(mturk_se,student_se),
                         model = c(rep("MTurk Sample", 19), rep("Student Sample", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() + 
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                              c("Culture", "Frequent Feedback", "Task Variety"),
                              c("Location", "Metro Area", "Rural Area"),
                              c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                              c("Salary", "$90,000", "$105,000"),
                              c("Size", "2,500 Employees", "500,000 Employees")))
p1

##Make Figure A6
pdf("rr_choose.pdf",width=12,height=12)
print(p1)
dev.off()

##Make Figure 1 (Right-hand Panel)
#Only Backsliding
results_df1 <- subset(results_df,attribute=="Backsliding")
pdf("rr_choose_backsliding.pdf",width=8,height=4)
dwplot(results_df1,dot_args = list(aes(shape = model))) +
  theme_bw() + 
  theme(legend.justification=c(0,0), legend.position=c(0,0),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14),
        plot.title = element_text(hjust = 0.5)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("Job Choice Outcome")
dev.off()


#######################
###Conditional AMCEs###
#######################
##Mturk Sample, Rating DV (by Party ID) [Figures 2 (left-hand panel) A3 and Table A4]
results <- amce(c_rate~Party*Salary+Party*Size+Party*Location+Party*Culture+Party*Partisanship+Party*Backsliding, data=mturk,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Party")
summary(results)
attribute <-as.vector(summary(results)$Party1amce[1])$Attribute
level <- as.vector(summary(results)$Party1amce[2])$Level
republican_acme <- as.vector(summary(results)$Party3amce[3])$Estimate
republican_se <- as.vector(summary(results)$Party3amce[4])$`Std. Err`
independent_acme <- as.vector(summary(results)$Party2amce[3])$Estimate
independent_se <- as.vector(summary(results)$Party2amce[4])$`Std. Err`
democrat_acme <- as.vector(summary(results)$Party1amce[3])$Estimate
democrat_se <- as.vector(summary(results)$Party1amce[4])$`Std. Err`

results_df <- data.frame(attribute = rep(attribute, times = 3),
                         term = rep(level, times = 3),
                         estimate = c(republican_acme, independent_acme,democrat_acme),
                         std.error = c(republican_se, independent_se,democrat_se),
                         model = c(rep("Republicans", 19), rep("Independents", 19),rep("Democrats", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() +
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                              c("Culture", "Frequent Feedback", "Task Variety"),
                              c("Location", "Metro Area", "Rural Area"),
                              c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                              c("Salary", "$90,000", "$105,000"),
                              c("Size", "2,500 Employees", "500,000 Employees")))
p1

#Make Figure A3
pdf("rr_rate_mturk_party.pdf",width=12,height=12)
print(p1)
dev.off()

#Make Figure 2 (Left-Hand Panel)
#Only Backsliding
results_df1 <- subset(results_df,attribute=="Backsliding")
pdf("rr_rate_mturk_party_backsliding.pdf",width=8,height=6)
dwplot(results_df1,dot_args = list(aes(shape = model))) +
  theme_bw() + 
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14),
        plot.title = element_text(hjust = 0.5)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("Job Rating Outcome")
dev.off()


##Mturk Sample, Choose DV (by Party ID) [Figures 2 Right-Hand panel and A7; Table A8]
results <- amce(c_choose~Party*Salary+Party*Size+Party*Location+Party*Culture+Party*Partisanship+Party*Backsliding, data=mturk,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Party")
summary(results)
attribute <-as.vector(summary(results)$Party1amce[1])$Attribute
level <- as.vector(summary(results)$Party1amce[2])$Level
republican_acme <- as.vector(summary(results)$Party3amce[3])$Estimate
republican_se <- as.vector(summary(results)$Party3amce[4])$`Std. Err`
independent_acme <- as.vector(summary(results)$Party2amce[3])$Estimate
independent_se <- as.vector(summary(results)$Party2amce[4])$`Std. Err`
democrat_acme <- as.vector(summary(results)$Party1amce[3])$Estimate
democrat_se <- as.vector(summary(results)$Party1amce[4])$`Std. Err`

results_df <- data.frame(attribute = rep(attribute, times = 3),
                         term = rep(level, times = 3),
                         estimate = c(republican_acme, independent_acme,democrat_acme),
                         std.error = c(republican_se, independent_se,democrat_se),
                         model = c(rep("Republicans", 19), rep("Independents", 19),rep("Democrats", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() +
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                              c("Culture", "Frequent Feedback", "Task Variety"),
                              c("Location", "Metro Area", "Rural Area"),
                              c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                              c("Salary", "$90,000", "$105,000"),
                              c("Size", "2,500 Employees", "500,000 Employees")))
p1

##Make Figure A7
pdf("rr_choose_mturk_party.pdf",width=12,height=12)
print(p1)
dev.off()

##Make Figure 2 (Right-hand Panel)
#Only Backsliding
results_df1 <- subset(results_df,attribute=="Backsliding")
pdf("rr_choose_mturk_backsliding.pdf",width=8,height=6)
dwplot(results_df1,dot_args = list(aes(shape = model))) +
  theme_bw() +
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14),
        plot.title = element_text(hjust = 0.5)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("Job Choice Outcome")
dev.off()


##Student Sample, Rating DV (by Party ID) [Figure A4; Table A5]
results <- amce(c_rate~Party*Salary+Party*Size+Party*Location+Party*Culture+Party*Partisanship+Party*Backsliding, data=student,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Party")
summary(results)
attribute <-as.vector(summary(results)$Party1amce[1])$Attribute
level <- as.vector(summary(results)$Party1amce[2])$Level
republican_acme <- as.vector(summary(results)$Party3amce[3])$Estimate
republican_se <- as.vector(summary(results)$Party3amce[4])$`Std. Err`
independent_acme <- as.vector(summary(results)$Party2amce[3])$Estimate
independent_se <- as.vector(summary(results)$Party2amce[4])$`Std. Err`
democrat_acme <- as.vector(summary(results)$Party1amce[3])$Estimate
democrat_se <- as.vector(summary(results)$Party1amce[4])$`Std. Err`

results_df <- data.frame(attribute = rep(attribute, times = 3),
                         term = rep(level, times = 3),
                         estimate = c(republican_acme, independent_acme,democrat_acme),
                         std.error = c(republican_se, independent_se,democrat_se),
                         model = c(rep("Republicans", 19), rep("Independents", 19),rep("Democrats", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() +
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                              c("Culture", "Frequent Feedback", "Task Variety"),
                              c("Location", "Metro Area", "Rural Area"),
                              c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                              c("Salary", "$90,000", "$105,000"),
                              c("Size", "2,500 Employees", "500,000 Employees")))
p1

##Make Figure A4
pdf("rr_rate_student_party.pdf",width=12,height=12)
print(p1)
dev.off()



##Student Sample, Choose DV (by Party ID) [Figure A8; Table A9]
results <- amce(c_choose~Party*Salary+Party*Size+Party*Location+Party*Culture+Party*Partisanship+Party*Backsliding, data=student,
                cluster=TRUE, respondent.id="respondent", respondent.varying = "Party")
summary(results)
attribute <-as.vector(summary(results)$Party1amce[1])$Attribute
level <- as.vector(summary(results)$Party1amce[2])$Level
republican_acme <- as.vector(summary(results)$Party3amce[3])$Estimate
republican_se <- as.vector(summary(results)$Party3amce[4])$`Std. Err`
independent_acme <- as.vector(summary(results)$Party2amce[3])$Estimate
independent_se <- as.vector(summary(results)$Party2amce[4])$`Std. Err`
democrat_acme <- as.vector(summary(results)$Party1amce[3])$Estimate
democrat_se <- as.vector(summary(results)$Party1amce[4])$`Std. Err`

results_df <- data.frame(attribute = rep(attribute, times = 3),
                         term = rep(level, times = 3),
                         estimate = c(republican_acme, independent_acme,democrat_acme),
                         std.error = c(republican_se, independent_se,democrat_se),
                         model = c(rep("Republicans", 19), rep("Independents", 19),rep("Democrats", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() +
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated ACME") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
p
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                        c("Culture", "Frequent Feedback", "Task Variety"),
                        c("Location", "Metro Area", "Rural Area"),
                        c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                        c("Salary", "$90,000", "$105,000"),
                        c("Size", "2,500 Employees", "500,000 Employees")))
p1
pdf("rr_choose_student_party.pdf",width=12,height=12)
print(p1)
dev.off()

##Differences in AMCEs [Figures A5 and A9]
x1 <- amce_diffs(mturk, c_choose~Salary+Size+Location+Culture+Partisanship+Backsliding, ~ Party, id = ~ respondent)
diffs_choose_mturk <- subset(x1, BY=="Republican - Democrat")

x1 <- amce_diffs(mturk, c_rate~Salary+Size+Location+Culture+Partisanship+Backsliding, ~ Party, id = ~ respondent)
diffs_rate_mturk <- subset(x1, BY=="Republican - Democrat")

x1 <- amce_diffs(student, c_choose~Salary+Size+Location+Culture+Partisanship+Backsliding, ~ Party, id = ~ respondent)
diffs_choose_student <- subset(x1, BY=="Republican - Democrat")

x1 <- amce_diffs(student, c_rate~Salary+Size+Location+Culture+Partisanship+Backsliding, ~ Party, id = ~ respondent)
diffs_rate_student <- subset(x1, BY=="Republican - Democrat")

##Figure: Rate DV [Figure A5]
attribute <-as.vector(diffs_rate_student$feature)
level <- as.vector(diffs_rate_student$level)
student_acme <- as.vector(diffs_rate_student$estimate)
student_se <- as.vector(diffs_rate_student$std.error)
mturk_acme <- as.vector(diffs_rate_mturk$estimate)
mturk_se <- as.vector(diffs_rate_mturk$std.error)

results_df <- data.frame(attribute = rep(attribute, times = 2),
                         term = rep(level, times = 2),
                         estimate = c(mturk_acme,student_acme),
                         std.error = c(mturk_se,student_se),
                         model = c(rep("MTurk Sample", 19), rep("Student Sample", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() + 
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated Difference in ACME, Republican - Democrat") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
p
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                              c("Culture", "Frequent Feedback", "Task Variety"),
                              c("Location", "Metro Area", "Rural Area"),
                              c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                              c("Salary", "$90,000", "$105,000"),
                              c("Size", "2,500 Employees", "500,000 Employees")))
p1

##Make Figure A5
pdf("rr_rate_diffs.pdf",width=12,height=12)
print(p1)
dev.off()


##Figure: Rate DV [Figure A9]
attribute <-as.vector(diffs_rate_student$feature)
level <- as.vector(diffs_rate_student$level)
student_acme <- as.vector(diffs_choose_student$estimate)
student_se <- as.vector(diffs_choose_student$std.error)
mturk_acme <- as.vector(diffs_choose_mturk$estimate)
mturk_se <- as.vector(diffs_choose_mturk$std.error)


results_df <- data.frame(attribute = rep(attribute, times = 2),
                         term = rep(level, times = 2),
                         estimate = c(mturk_acme,student_acme),
                         std.error = c(mturk_se,student_se),
                         model = c(rep("MTurk Sample", 19), rep("Student Sample", 19)),
                         stringsAsFactors = FALSE)
results_df$term <- factor(results_df$term,levels=c("Corruption","Governor","Protest","Redistricting","Unionize","Voter ID","Frequent Feedback","Great Talent","Task Variety","Metro Area","Midsize City","Rural Area",
                                                   "Weak Clinton State","Weak Trump State","Strong Trump State","$90,000","$105,000","2,500 Employees","500,000 Employees"),ordered=T)
# Draw dot-and-whisker plot
p <- dwplot(results_df,dot_args = list(aes(shape = model))) +
  theme_bw() + 
  theme(legend.justification=c(1,1), legend.position=c(1,1),
        legend.title = element_blank(), legend.background = element_rect(color="black",size=.25),
        axis.text.x=element_text(colour="black"),
        axis.text.y=element_text(colour="black"),
        panel.border = element_rect(colour = "black"),
        text = element_text(size=14)) + 
  guides(shape = guide_legend(reverse = TRUE), colour = guide_legend(reverse = TRUE)) +
  xlab("Estimated Difference in ACME, Republican - Democrat") + scale_colour_grey(start = .2, end = .7) +
  geom_vline(xintercept = 0, colour = "black", linetype = 2) +
  ggtitle("")
p
# Add brackets
p1 <- p %>% add_brackets(list(c("Backsliding", "Corruption", "Voter ID"), 
                              c("Culture", "Frequent Feedback", "Task Variety"),
                              c("Location", "Metro Area", "Rural Area"),
                              c("Partisanship", "Weak Clinton State", "Strong Trump State"),
                              c("Salary", "$90,000", "$105,000"),
                              c("Size", "2,500 Employees", "500,000 Employees")))
p1

##Make Figure A9
pdf("rr_choose_diffs.pdf",width=12,height=12)
print(p1)
dev.off()

